﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.DataEntity;
using TimKen.BussLogic;
using System.Data;


namespace TimKen.Web.Admin.IndustrySolution
{
    public partial class IndustrySolution_Add : TimKen.App.AdminPage
    {
        public override string ActionCode
        {
            get
            {
                return "32";//成功案例
            }
        }

        public int ArticleID
        {
            get
            {
                if (ViewState["ArticleID"] != null)
                    return Convert.ToInt32(ViewState["ArticleID"]);
                else
                    return 0;
            }
            set
            {
                ViewState["ArticleID"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindResources();

                if (Request.QueryString["ArticleID"] != null)
                {
                    ArticleID = Convert.ToInt32(Request.QueryString["ArticleID"]);
                }
                if (ArticleID != 0)
                    BindData();
            }
        }

        protected void BindResources()
        {
            ddlLanguage.Items.Clear();
            ListItem li_ZH = new ListItem("简体中文", "1");
            ListItem li_EN = new ListItem("English", "2");
            ddlLanguage.Items.Add(li_ZH);
            ddlLanguage.Items.Add(li_EN);
            btnConfirm.Text = GetLangString("Save");
        }

        private void BindData()
        {
            ArticleManager am = new ArticleManager();
            DataEntity.Article article = am.GetModel(ArticleID);
            this.txtTitle.Text = article.Title;
            this.txtAuthor.Text = article.Author;

            if (ddlLanguage.SelectedItem != null)
                ddlLanguage.SelectedItem.Selected = false;

            if (article.LanguageID == 1)
                ddlLanguage.Items[0].Selected = true;
            else
                ddlLanguage.Items[1].Selected = true;

            this.fckContent.Value = article.Content;

            this.txtCreateDate.Text = article.CreateDate.Value.ToString("yyyy-MM-dd");
            this.txtCreateDate.ReadOnly = true;
        }
        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            DataEntity.Article article = new TimKen.DataEntity.Article();
            ArticleManager am = new ArticleManager();

            //新增成功案例
            if (ArticleID == 0)
            {

                article.Author = this.txtAuthor.Text.Trim();
                article.CreateDate = DateTime.Now;
                article.Content = this.fckContent.Value;
                article.Title = this.txtTitle.Text;
                article.LanguageID = this.ddlLanguage.SelectedValue == "1" ? 1 : 2;
                article.TypeID = 3;
                am.Add(article);

            }
            else
            {//更新成功案例
                article.ID = ArticleID;
                article.Author = this.txtAuthor.Text.Trim();
                article.CreateDate = DateTime.Now;
                article.Content = this.fckContent.Value;
                article.Title = this.txtTitle.Text;
                article.LanguageID = this.ddlLanguage.SelectedValue == "1" ? 1 : 2;
                article.TypeID = 3;
                am.Update(article);
            }

            Response.Redirect("IndustrySolution_List.aspx");
        }



    }
}
